import csv
import chardet


def detect_encoding(file_path):
    """检测文件编码"""
    with open(file_path, 'rb') as file:
        return chardet.detect(file.read())['encoding']


def query_consumption(file_path, province, *items):
    """查询并显示指定省份和消费项目的消费金额"""
    encoding = detect_encoding(file_path)
    with open(file_path, mode='r', encoding=encoding) as file:
        reader = csv.DictReader(file)
        results = {item: None for item in items}
        for row in reader:
            if row['地区'] == province:
                print(row)
                for item in items:
                    if item in row:
                        results[item] = row[item]
                if all(results.values()):
                    break
        print(results)

    output = f"你输入的省份是：{province}，"
    for item, amount in results.items():
        if amount is not None:
            output += f"{item}的消费金额为：{amount}，"
        else:
            output += f"{item}的信息未找到，"
    return output.rstrip(",") + "。"


# 文件路径
file_path = 'xiaofei.csv'

province = input("请输入省份名称：")
items_input = input("请输入需要查找的项目，用逗号分隔：").split(",")
items = [item.strip() for item in items_input]

result = query_consumption(file_path, province, *items)
print(result)